<?php

namespace app\index\controller;

class Tool extends Common
{
    /**
     * 阿里云OSS图片上传公共接口，支持单张或多张上传
     */
    public function upLoadToOss()
    {
        $file = $_FILES;
        $data = array();
        if (!$file) {
            $this->rest("上传文件错误", "s004");
        }
        $fileName = $file["file"]["name"];
        $filePath = $file["file"]["tmp_name"];
        if (is_array($fileName)) {
            for ($i = 0; $i < count($fileName); $i++) {
                $explode = explode('.', $fileName[$i]);
                $saveName = uniqid() . '.' . $explode[1];
                $subPath = date('Ymd') . '/';
                $saveFileName = $subPath . $saveName;
                $data[] = ailOssUpfile($saveFileName, $filePath[$i])["oss-request-url"];
            }
        } else {
            $explode = explode('.', $fileName);
            $saveName = uniqid() . '.' . $explode[1];
            $subPath = date('Ymd') . '/';
            $saveFileName = $subPath . $saveName;
            $data[] = ailOssUpfile($saveFileName, $filePath)["oss-request-url"];
        }
        if (count($data) > 0) {
            $this->rest($data);
        } else {
            $this->rest("上传文件错误", "s004");
        }
    }

    /**
     * 腾讯云OSS图片上传公共接口，支持单张或多张上传
     */
    public function upLoadToTxOss()
    {
        $file = $_FILES;
        $data = array();
        if (!$file) {
            $this->rest("上传文件错误", "s004");
        }
        $fileName = $file["file"]["name"];
        $filePath = $file["file"]["tmp_name"];
        if (is_array($fileName)) {
            for ($i = 0; $i < count($fileName); $i++) {
                if ($filePath[$i] && $fileName[$i]) {
                    $explode = explode('.', $fileName[$i]);
                    $saveName = uniqid() . '.' . $explode[1];
                    $subPath = date('Ymd') . '/';
                    $saveFileName = $subPath . $saveName;
                    $data[] = txOssUpFile($saveFileName, $filePath[$i]);
                }
            }
        } else {
            if ($filePath && $fileName) {
                $explode = explode('.', $fileName);
                $saveName = uniqid() . '.' . $explode[1];
                $subPath = date('Ymd') . '/';
                $saveFileName = $subPath . $saveName;
                $data[] = txOssUpFile($saveFileName, $filePath);
            }
        }
        if (count($data) > 0) {
            $this->rest($data);
        } else {
            $this->rest("上传文件错误", "s004");
        }
    }


    /**
     * 获取天气预报
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWeather()
    {
        // 查询数据库是否有当天的天气数据
        $db = db("weather");
        $data = $db->whereTime('updatetime', 'today')->find();
        $key = config('config.amap')['key'];
        $adcode = input('adcode') ? input('adcode') : '522600';
        if (!$data) {
//            $weather = file_get_contents('http://t.weather.sojson.com/api/weather/city/101260501');
            $weather = file_get_contents("https://restapi.amap.com/v3/weather/weatherInfo?city=$adcode&key=$key&extensions=all");
            $db->insert(["data" => $weather]);
        } else {
            $weather = $data["data"];
            // 更新时段
            $people = [4, 9, 14, 20];
            // 判断是否在更新时间内
            $hour = date("G", time());
            if (in_array($hour, $people)) {
                $updatetime = date("G", strtotime($data['updatetime']));
                //判断这个时段内是否更新过
                if (!in_array($updatetime, $people)) {
//                    $weather = file_get_contents('http://t.weather.sojson.com/api/weather/city/101260501');
                    $weather = file_get_contents("https://restapi.amap.com/v3/weather/weatherInfo?city=$adcode&key=$key&extensions=all");
                    $db->insert(["data" => $data]);
                }
            }
        }
        if ($weather) {
            $this->rest(json_decode($weather, true)['forecasts']);
        } else {
            $this->rest('获取数据错误', 's004');
        }
    }


    /**
     * 根据坐标获取地区数据
     */
    public function getLocation()
    {
        $key = config('config.amap')['key'];
        $longitude = input('longitude');
        $latitude = input('latitude');
        $url = "https://restapi.amap.com/v3/geocode/regeo?key=$key&location=$longitude,$latitude";
        $location = file_get_contents($url);
        if ($location) {
            $addressComponent = json_decode($location, true)['regeocode']['addressComponent'];
            $rest = array(
                "city" => $addressComponent['city'],
                "province" => $addressComponent['province'],
                "adcode" => $addressComponent['adcode'],
                "district" => $addressComponent['district']
            );
            $this->rest($rest);
        } else {
            $this->rest('获取数据错误', 's004');
        }
    }
}
